import { PokerHand, cards2letter } from "./PokerHand.js";
import { PokerCard } from "../PokerCard.js";
export type { PokerCard, PokerType } from "../PokerCard.js";

/** 四带二单 */
export class FourWithSinglePokerHand extends PokerHand {
  public value: number;
  constructor(cards: PokerCard[]) {
    super(cards);

    /** 临时对象 */
    const tmp: any = {};
    cards.forEach((card) => {
      if (tmp[card.value]) {
        tmp[card.value] += 1;
      } else {
        tmp[card.value] = 1;
      }
    });
    this.value = Number(Object.keys(tmp).find((key) => tmp[key] === 4));
  }

  /**
   * 判断是否四带二单：四张牌＋两单牌。（注意：四带二不是炸弹）。
   * @param cards 手牌
   * @returns
   */
  public static isStandard(cards: PokerCard[]) {
    if (cards.length !== 6) {
      return false;
    }

    const str = cards2letter(cards);
    return /([A-O])\1{3}/g.test(str);
  }
  public isStandard = FourWithSinglePokerHand.isStandard;

  public isBetter(target: PokerHand): boolean {
    if (target instanceof FourWithSinglePokerHand) {
      return this.value > target.value;
    }

    return false;
  }
}

export default FourWithSinglePokerHand;
